<?php

namespace Common\Model;

use Think\Model;

class ConfigModel extends Model {

    private $cache_time = 86400;

    function setConfig($key, $val) {

        $data = array(
            'key' => $key,
            'value' => json_encode($val),
            'update_time' => time(),
        );
        $result = $this->add($data, '', true);
        if ($result)
            S($cache_key, $val, $this->cache_time);
        return $result;
    }

    function getConfig($key) {
        $cache_key = $key;
        $result = S($cache_key);
        if (!$result) {
            $result = $this->find($key);
            if (!$result)
                return array();

            $result = json_decode($result['value'], true);
            S($cache_key, $result, $this->cache_time);
        }
        return $result;
    }

}
